package com.rytong.tools.ui.chart;

import java.util.ArrayList;
import java.util.List;

import com.rytong.tools.ui.Component;
import com.rytong.tools.ui.CssStyle;
import com.rytong.tools.ui.LPLayout;
import com.rytong.tools.ui.LPScrollView;
import com.rytong.tools.ui.LPLayout.MyLPLayout;
import com.rytong.tools.utils.Utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.view.MotionEvent;
import android.view.View;

/**
 * @author lixiao x轴控�?
 */
public class LPXScale extends Component {

    // 字体大小
    int fontSize_;
    // 字体高度
    int fontHeight_;
    // x轴的高度
    int height_;
    // x轴刻度字体颜�?
    int cs_fontColor_;
    // x轴刻度线颜色
    int cs_lineColor_;
    // x轴刻度线的宽
    int cs_lineWidth_;
    // 刻度值内�?
    List<String> lineValue_;
    //刻度值之间间�?
    float valueSpace_;
    //刻度�?
    int size_;
    // y轴文字最大高度
    int maxFontHeight_;
    // 字体间距
    int TEXT_SPACE = Utils.getScaledValueY(2);
    
    public class MyLPXScale extends View implements CompositedComponent {

        public MyLPXScale(Context context, String value) {
            super(context);
        }

        @Override
        public Component composited() {
            return LPXScale.this;
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            // 画刻度线
            drawLine(canvas);
            // 画刻度�?
            drawValue(canvas);

        }

        private void drawValue(Canvas canvas) {
            paint_.setColor(cs_fontColor_);
            for (int i = 0; i < size_; i++) {
                char[] texts = lineValue_.get(i).toCharArray();
                int count = texts.length;
                for (int j = 0; j < count; j++) {
                    canvas.drawText(String.valueOf(texts[j]),
                    		i * valueSpace_ + valueSpace_ / 2 - paint_.measureText("国") / 2,
                    		(j + 1) * (fontHeight_ + TEXT_SPACE) + TEXT_SPACE, paint_);
                }
            }
        }

        private void drawLine(Canvas canvas) {
            paint_.setStrokeWidth(cs_lineWidth_);
            paint_.setColor(cs_lineColor_);
            canvas.drawLine(0, 0, width_, 0, paint_);
        }
        
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            	// 按下tr时候变色。
            	trDownColor(tr_, llvSV_);
                xDown_ = event.getX();
                yDown_ = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
            	// 移动tr恢复颜色。
            	int offset = (int) Math.abs(yDown_ - event.getY());
            	if (offset > offset_) {
            		trMoveColor(tr_, llvSV_);
            	}
                moveY(yDown_, event.getY());
                break;
            }
            return super.onTouchEvent(event);
        }

    }

    @Override
    public void initRealView(Activity activity, String nodeValue) {
        super.initRealView(activity, nodeValue);
        realView_ = new MyLPXScale(activity, nodeValue);
        childrenList_ = new ArrayList<Component>();
    }

    @Override
    public void mouldH() throws Exception {
        super.mouldH();
        mouldChildList();
        // 设置字体大小
        String fontSizeStr = (String) cssStyle_.getProperty(CssStyle.FONTSIZE);
        if (null != fontSizeStr && !fontSizeStr.equals("")) {
            fontSize_ = Integer.parseInt(fixedPX(fontSizeStr));
            fontSize_ = Utils.getFontSize(fontSize_);
        }
        paint_ = new Paint();
        paint_.setAntiAlias(true);
        paint_.setTextSize(Utils.getScaledValueX(fontSize_));
        FontMetrics fm = paint_.getFontMetrics();
        fontHeight_ = (int) (Math.ceil(fm.descent - fm.ascent)); // 计算文字的高�?
//        fontHeight_ = ((int) (paint_.getTextSize() * 3)) >> 2;
        // x轴高度暂设置为字体高度的3�?
//        height_ = 3 * fontHeight_;
        
        int size = childrenList_.size();
        String nodeValueTemp = null;
        for (int i = 0; i < size; i++) {
            // 计算y轴刻度坐标最大宽�?
            nodeValueTemp = (String) ((Component)childrenList_.get(i)).property_.get("text");
            if(nodeValueTemp == null)
                continue;
            int length = nodeValueTemp.toCharArray().length;
            maxFontHeight_ = Math.max(maxFontHeight_, length * fontHeight_+(length + 1) * TEXT_SPACE);
        }
        height_ = maxFontHeight_;
        
        // 获取字体颜色
        String fontColor = (String) cssStyle_.getProperty(CssStyle.COLOR);
        if (null != fontColor && !fontColor.equals("")) {
            cs_fontColor_ = Utils.getColorValue(fontColor);
        }
        // 获取刻度线�?�色
        String lineColor = (String) cssStyle_.getProperty(CssStyle.BACKGROUNDCOLOR);
        if (null != lineColor && !lineColor.equals("")) {
            cs_lineColor_ = Utils.getColorValue(lineColor);
        }
        // 获取刻度线宽�?
        String lineWidth = (String) cssStyle_.getProperty(CssStyle.WIDTH);
        if (null != lineWidth && !lineWidth.equals("")) {
            cs_lineWidth_ = Integer.parseInt(fixedPX(lineWidth));
            cs_lineWidth_ = Utils.getScaledValueY(cs_lineWidth_);
        }
        // 获取刻度值内�?
        lineValue_ = new ArrayList<String>();
        for (int i = 0; i < childrenList_.size(); i++) {
            lineValue_.add((String) ((Component) childrenList_.get(i)).property_.get("text"));
        }
        //获取刻度�?
        size_ = childrenList_.size();
        //获取刻度间距
        String valuespace = (String) cssStyle_.getProperty(CssStyle.MARGINTOP);
        if (null != valuespace && !valuespace.equals("")) {
            int valuespan = Integer.parseInt(fixedPX(valuespace));
            valueSpace_ = Utils.getScaledValueX(valuespan);
        }
        width_ = (int) (size_ * valueSpace_);
    }
    
    @Override
    public void setBGColor() {
        // x轴的BACKGROUNDCOLOR指的是刻度线的颜色，所以不能使用Component中的setBGColor方法
    }

    @Override
    public void releaseResource(Component component) {
    	super.releaseResource(component);
    	if(lineValue_ != null){
    		lineValue_.clear();
    		lineValue_ = null;
    	}
    }
}
